package com.hongtao3.hsync.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;

import com.hongtao3.hsync.ModifiedFlowLayout;

abstract public class ResourcesPanel extends JPanel {
    public ResourcesPanel(){
    	setLayout(new BorderLayout());
    	addTopPanel();
    	addCenterPanel();
    }
    private void addTopPanel(){
        JPanel p=new JPanel(); 
        SpringLayout layout=new SpringLayout();
        p.setLayout(layout);
        JLabel l1=new JLabel("path:");
        layout.putConstraint(SpringLayout.NORTH, l1, 0, SpringLayout.NORTH, p);
 		layout.putConstraint(SpringLayout.SOUTH, l1, 0, SpringLayout.SOUTH, p);
 		layout.putConstraint(SpringLayout.WEST, l1, 0, SpringLayout.WEST, p);
 		layout.putConstraint(SpringLayout.EAST, l1, 50, SpringLayout.WEST, p);
        p.add(l1);
        p.setPreferredSize(new Dimension(100, 25));
     	pathBox=new JComboBox();
     	layout.putConstraint(SpringLayout.WEST, pathBox, 50, SpringLayout.WEST, p);
     	layout.putConstraint(SpringLayout.SOUTH, pathBox, 0, SpringLayout.SOUTH, p);
     	layout.putConstraint(SpringLayout.EAST, pathBox, 0, SpringLayout.EAST, p);
     	p.add(pathBox);
     	pathBox.setEditable(true);
     	add(p,BorderLayout.NORTH);
     	
     	pathBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String path=(String)pathBox.getSelectedItem();
 				if(path==null)return;
 				File f=getFile(path);
 				if(f.exists() && f.isDirectory()){
 					repaintPanel(path);
 				}
				
			}
		});
     }
    

    private void addCenterPanel(){
    	centerPanel=new JPanel();
    	centerPanel.setLayout(new ModifiedFlowLayout(FlowLayout.LEFT));
    	JScrollPane sp=new JScrollPane(centerPanel);
    	add(sp,BorderLayout.CENTER);
    }
    public void showPath(String path){
    	pathBox.removeAllItems();
    	pathBox.addItem(path);
    }
    
    public File getFile(String path){
    	return new File(path);
    }
    
    protected void repaintPanel(String path){
    	File f=getFile(path);
    	if(!f.exists() || f.isHidden())return;
    	centerPanel.removeAll();
    	centerPanel.add(new FileButton(getFile(path+"/../"),this));
    	if(f.isFile()){
    		centerPanel.add(new FileButton(f,this));
    	}else if(f.isDirectory()){
    		File[] files=f.listFiles();
    	    filesSort(files);
    		for(File _f:files){
    			if(_f.isHidden())continue;
    			centerPanel.add(new FileButton(_f,this));
    		}
    	}
    	centerPanel.repaint();
    }
    
    protected void filesSort(File[] files){
    	Arrays.sort(files, new Comparator() {
    		public int compare(final Object o1, final Object o2) {
    			return ((File)o2).getName().toLowerCase().compareTo(((File)o1).getName().toLowerCase());
    		}
    	}); 
    	Arrays.sort( files, new Comparator() {
    		public int compare(final Object o1, final Object o2) {
    			return ((File)o2).isDirectory()?1:0;
    		}
    	}); 
    }
    
    private JComboBox pathBox;
    private JPanel centerPanel;
}
